package aom.ju.ss.view.base

import android.content.Intent
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.UpImgSBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.LpKey
import aom.ju.ss.mode.UpImgMode
import aom.ju.ss.utils.*
import aom.ju.ss.utils.CameraUtils.Companion.isNew
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.dialog.PicCameraDialog
import com.bumptech.glide.Glide
import com.google.gson.Gson

/**
 * 上传文件 ListFragment
 */
class UploadFilesFragment : BaseListFragment() {
    private var cameraUtils: CameraUtils? = null
    private var max = 10
    fun setMax(max: Int) {
        this.max = max
        pAdapter.bindViewInterface
    }

    init {
        isHttp = false
    }

    override fun onBaseCreateView() {
        super.onBaseCreateView()
        binding?.small?.isEnabled = false
        cameraUtils = isNew(this)
        getList().add("")
        pAdapter.notifyDataSetChanged()
    }

    override fun bind(binding: ViewDataBinding, position: Int) {
        super.bind(binding, position)
        val upImgSBinding = binding as UpImgSBinding
        upImgSBinding.max = max.toString()
        if (position == getList().size - 1) {
            upImgSBinding.numtv = (position).toString()
        }
        upImgSBinding.num.VG(position == getList().size - 1)
        upImgSBinding.del.VG(getList()[position] != "")

        if (getList()[position] == "") {
            upImgSBinding.img.setImageResource(R.drawable.ic_up_img)
        } else {
            Glide.with(this)
                .load(getList()[position].toString())
                .centerCrop()
                .into(upImgSBinding.img)
        }
        if (position == getList().size - 1) {

            upImgSBinding.img.setOnClickListener {
                if (getList().size != max) {
                    PicCameraDialog(
                        mContext!!,
                        object : TitleDialogInterface {
                            override fun ok(view: View?, IsDetermine: Boolean) {
                                if (IsDetermine) {
                                    cameraUtils!!.show()
                                } else {
                                    cameraUtils!!.showPic()
                                }
                            }
                        })
                } else {
                    show("最多上传" + (max) + "张图片~")
                }
            }
        } else {

            upImgSBinding.del.setOnClickListener {
                getList().removeAt(position)
                pAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun item(): Int {
        return R.layout.up_img_s
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val file = cameraUtils!!.onActivityResult(requestCode, resultCode, data)
        if (file != null) {
            LogUtils.e(file.absolutePath)
            OKHttpUtils(activity)
                .setLoadingStr("上传中...")
                .SetApiUrl(LP_API.upload_image)
                .SetKey("type")
                .SetValue("def")
                .setFile(file)
                .UpFile(object : MyHttpCallBack {
                    override fun ok(jsonString: String?, httpTY: String?) {
                        val mode = Gson().fromJson(jsonString, UpImgMode::class.java)
                        getList().add(0, mode.data.path)
                        var tag = "0"
                        binding?.rootView?.tag?.let {
                            tag = it.toString()
                        }
                        SendMsg.sendEvent(
                            LpKey.UP_IMG_OK,
                            Gson().toJson(UpMode(tag, getList() as ArrayList<String>))
                        )

                        pAdapter.notifyDataSetChanged()
                    }

                    override fun error(e: String?, jsonString: String?, httpTY: String?) {
                        e?.show
                    }

                })

        } else {
            "获取图片失败".show
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        cameraUtils!!.onPermissions(requestCode, permissions, grantResults)
    }

    override fun LayoutManager(): RecyclerView.LayoutManager {
        return StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL)
    }
}

data class UpMode(var tag: String = "", var list: ArrayList<String> = arrayListOf())